package cclub.demo.Interceptor;

import cclub.demo.config.SecretConfig;
import cclub.demo.service.intf.userService;
import cclub.demo.service.secretService;
import com.cclub.inteface.service.common.redisTools;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Slf4j
public class CclubInterceptor implements HandlerInterceptor{

    @Autowired
    private userService userService;

    @Autowired
    private secretService  secretService;

    @Autowired
    private SecretConfig secretConfig;

    @Reference(version = "1.0.0")
    private redisTools redisTools;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("请求进入拦截器");
        String token = request.getHeader("token");
        String sign = request.getHeader("sign");
        String data = request.getParameter("data");
        //如果不是映射到方法直接通过
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        Method method = handlerMethod.getMethod();
//        if(!method.isAnnotationPresent(PassToken.class)){
//            if(StringUtils.isEmpty(token) || StringUtils.isEmpty(data)){
//                throw new InvalidParameterException("请求参数中token,data不能为空");
//            }
//            //验签
//            String userId;
//            try{
//                userId = JWT.decode(token).getAudience().get(0);
//            }catch (JWTDecodeException e){
//                throw new SysException();
//            }
//            userDto userDto = userService.getUserInfo(Integer.parseInt(userId));
//            if(userDto == null){
//                throw new InvalidParameterException("未找到对应的用户");
//            }
//            if(redisTools.getCache(cacheKey.USER_INFO+userId) == null){
//                throw new NoAuthException("当前token无效请重新登录");
//            }
//            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secretConfig.getPrivateKey())).build();
//            try {
//                jwtVerifier.verify(token);
//            } catch (JWTVerificationException e) {
//                throw new NoAuthException("当前token无效请重新登录");
//            }
//        }
        //数据解密
//        String decryptData = secretService.decryptAes(data);
//        if(StringUtils.isEmpty(decryptData)){
//            log.error("数据解密失败data={}",data);
//            throw new SysException("数据解密失败");
//        }
//        //把解密后的数据发给控制层
//        HashMap map = JSON.parseObject(data,HashMap.class);
//        for (Object key : map.keySet()){
//            request.setAttribute(key.toString(),map.get(key));
//        }
        return true;
    }
}
